<?php

namespace app\admin\controller\system;

use app\common\controller\Backend;
use think\facade\Db;

/**
 * 文件管理
 *
 */
class Files extends Backend {
    /**
     * Files模型对象
     * @var \app\admin\model\system\Files
     */
    protected object $model;

    protected array|string $preExcludeFields = ['id', 'create_time'];

    protected array|string $quickSearchField = ['name'];

    public function initialize():void {
        parent::initialize();
        $this->model = new \app\admin\model\system\Files;
    }

    public function index():void {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();

        $this->withJoinTable = ['category'];
        $res = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->paginate($limit);

        $list = $res->items();

        foreach($list as &$val){
            if($val['category'] && $val['category']['pid'] > 0){
                $val['category']['name'] = Db::name('system_files_category')->where(['id' => $val['category']['pid']])->value('name') . ' - ' . $val['category']['name'];
            }
        }

        $this->success('', [
            'list' => $list,
            'total' => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */
}